<template>
    <div class="good-list">
        <div class="good-item" v-for="item in goodslist" @click="goDetail(item.id)">
            <img :src="item.img_url" alt="">
            <h1 class="title">{{item.title}}</h1>
            <div class="info">
                <p class="price">
                    <span class="now">{{item.sell_price}}</span>
                    <span class="old">{{item.market_price}}</span>
                </p>
                <p class="sell">
                    <span>热卖中</span>
                    <span>剩余{{item.stock_quantity}}件</span>
                </p>
            </div>
        </div>

        <mt-button type="danger" size="large" @click="getmore"> 加载更多</mt-button>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                pageindex: 1,
                goodslist: [],
                // id:this.$route.params.id
            }
        },
        created() {
            this.getgoodslist();
        },

        methods: {
            getgoodslist() {
                this.$http.get("api/getgoods?pageindex=" + this.pageindex).then(result => {
                    if (result.body.status === 0) {
                        this.goodslist = this.goodslist.concat(result.body.message)
                    }
                })
            },
            getmore() {
                this.pageindex++;
                this.getgoodslist();
            },
            goDetail(id){
                //跳转的三种方式
                // 字符串
                // this.$router.push('/home/goodsinfo/'+id)
                // 对象
                // this.$router.push({ path: '/home/goodsinfo/'+id })
                // 命名的路由，name是 router.js 文件中路径 的 name
                this.$router.push({ name: 'goodsinfo', params: { id}})
            }



        }

    }
</script>


<style lang="scss" scoped>
    .good-list {
        background-color: #fff;
        display: flex;
        flex-wrap: wrap;

        padding: 7px;
        justify-content: space-between;
        .good-item {
            width: 49%;
            border: 1px solid #ccc;
            box-shadow: 0 0 5px #ccc;
            margin: 4px 0;
            padding: 2px;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            img {
                width: 100%;
                min-height: 173px;
            }
            .title {
                font-size: 14px;
            }
            .info {
                background-color: #eee;
                .price {
                    .now {
                        font-size: 16px;
                        color: red;
                        font-weight: bold;
                    }
                    .old {
                        font-size: 13px;
                        text-decoration: line-through;
                        margin-left: 10px;
                    }
                }
                .sell {
                    display: flex;
                    justify-content: space-between;
                }
            }
        }
    }
</style>